package com.gztms.usermgnt.controller;

import com.gztms.usermgnt.pojo.User;
import com.gztms.usermgnt.pojo.vo.PasswordVO;
import com.gztms.usermgnt.service.UserService;
import com.gztms.usermgnt.service.impl.UserServiceImpl;
import com.gztms.utils.ResultUtil;
import com.gztms.utils.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author Zoey
 * @date 2023/4/17 18:40
 * @Description TODO
 **/
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户注册
     *
     * @param user
     * @return
     */
    @PostMapping("/user/register")
    public ResultUtil register(
            @RequestBody User user
//            @RequestParam(value = "username") String username,
//            @RequestParam(value = "password") String password,
//            @RequestParam(value = "phone") String phone,
//            @RequestParam(value = "email") String email
    ) {
//        User user = new User();
//        user.setUid(phone);
//        user.setPassword(password);
//        user.setPhone(phone);
//        user.setEmail(email);
//        user.setUsername(username);
        return userService.register(user);
    }


    /**
     * 修改头像
     * @param file
     * @param uid
     * @return
     * @throws IOException
     */
    @PostMapping("user/updateAvatar")
    public ResultUtil updateAvatar(@RequestParam(value = "file") MultipartFile file, @RequestParam(value = "uid") String uid) throws IOException {
        // 处理图片
        String avatar = UploadUtil.uploadImage(file);
        return userService.updateAvatar(uid, avatar);
    }

    /**
     * 修改个人信息
     * @param user
     * @return
     */
    @PostMapping("/user/updateUserInfo")
    public ResultUtil updateUserInfo(@RequestBody User user){
        return userService.updateUserInfo(user);
    }

    /**
     * 查询用户个人详细信息
     * @param uid
     * @return
     */
    @GetMapping("/user/{uid}")
    public ResultUtil getUserInfoByUid(@PathVariable String uid) {
        return userService.getUserInfoByUid(uid);
    }

    /**
     * 修改密码
     * @param passwordVO
     * @return
     */
    @PostMapping("/user/updatePassword")
    public ResultUtil updatePassword(@RequestBody PasswordVO passwordVO) {
        return userService.updatePassword(passwordVO);
    }


}
